package com.wwc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwc.business.controller.dto.train_station.TrainStationDeleteDTO;
import com.wwc.business.controller.dto.train_station.TrainStationInsertDTO;
import com.wwc.business.controller.dto.train_station.TrainStationSelectListDTO;
import com.wwc.business.controller.dto.train_station.TrainStationUpdateDTO;
import com.wwc.business.controller.vo.TrainStationSelectListVO;
import com.wwc.business.entity.TrainStation;
import com.wwc.business.mapper.TrainStationMapper;
import com.wwc.business.service.ITrainStationService;
import com.wwc.common.exception.BusinessException;
import com.wwc.common.exception.BusinessExceptionEnum;
import com.wwc.common.response.PageResponse;
import com.wwc.common.utils.SnowUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TrainStationServiceImpl extends ServiceImpl<TrainStationMapper, TrainStation> implements ITrainStationService{
	@Resource
	private TrainStationMapper trainStationMapper;

	@Override
	public PageResponse<TrainStationSelectListVO> selectListPage(TrainStationSelectListDTO trainStationSelectListDTO){
		// 根据分页配置和查询条件获取火车车站分页列表
		Page<TrainStation> page = new Page<>(trainStationSelectListDTO.getPage(), trainStationSelectListDTO.getSize());
		QueryWrapper<TrainStation> trainStationQueryWrapper = new QueryWrapper<>();
		trainStationQueryWrapper.orderByAsc("train_code").orderByAsc("`index`");
		if(StrUtil.isNotBlank(trainStationSelectListDTO.getTrainCode())){
			trainStationQueryWrapper.eq("train_code", trainStationSelectListDTO.getTrainCode());
		}
		Page<TrainStation> trainStationPage = trainStationMapper.selectPage(page, trainStationQueryWrapper);

		// 返回自定义分页配置类
		PageResponse<TrainStationSelectListVO> pageResponse = new PageResponse<>();
		pageResponse.setTotal(trainStationPage.getTotal());
		pageResponse.setList(BeanUtil.copyToList(trainStationPage.getRecords(), TrainStationSelectListVO.class));
		return pageResponse;
	}

	@Override
	public void delete(TrainStationDeleteDTO trainStationDeleteDTO){
		// 判断想要删除的数据是否存在
		TrainStation ts = trainStationMapper.selectById(trainStationDeleteDTO.getId());
		if(ObjectUtil.isNull(ts)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DELETE_DATA_DOES_NOT_EXIST);
		}

		// 删除火车车站
		trainStationMapper.deleteById(trainStationDeleteDTO.getId());
	}

	@Override
	public void update(TrainStationUpdateDTO trainStationUpdateDTO){
		// 判断想要修改的数据是否存在
		TrainStation ts = trainStationMapper.selectById(trainStationUpdateDTO.getId());
		if(ObjectUtil.isNull(ts)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DATA_DOES_NOT_EXIST);
		}

		// 根据查询条件获取火车车站列表
		QueryWrapper<TrainStation> trainStationQueryWrapper = new QueryWrapper<>();
		trainStationQueryWrapper.ne("id", trainStationUpdateDTO.getId());
		List<TrainStation> trainStationList = trainStationMapper.selectList(trainStationQueryWrapper);

		// 循环火车车站列表，判断是否满足条件
		for(TrainStation t : trainStationList){
			// 获取 车次编号+站序
			String train_code_index = t.getTrainCode() + "-" + t.getIndex();
			// 获取 车次编号+站名
			String train_code_name = t.getTrainCode() + "-" + t.getName();

			// 获取新增 车次编号+站序
			String train_code_index_new = trainStationUpdateDTO.getTrainCode() + "-" + trainStationUpdateDTO.getIndex();
			// 获取新增 车次编号+站名
			String train_code_name_new = trainStationUpdateDTO.getTrainCode() + "-" + trainStationUpdateDTO.getName();

			// 判断是否存在，如果已存在则抛出异常
			if(train_code_index.equals(train_code_index_new)){
				throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_INDEX_UNIQUE_ERROR);
			}
			// 判断是否存在，如果已存在则抛出异常
			if(train_code_name.equals(train_code_name_new)){
				throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_NAME_UNIQUE_ERROR);
			}
		}

		// 修改火车车站
		TrainStation trainStation = BeanUtil.copyProperties(trainStationUpdateDTO, TrainStation.class);
		trainStation.setUpdateTime(new Date());
		trainStationMapper.updateById(trainStation);
	}

	@Override
	public void insert(TrainStationInsertDTO trainStationInsertDTO){
		// 获取火车车站列表
		List<TrainStation> trainStationList = trainStationMapper.selectList(null);

		// 循环火车车站列表，判断是否满足条件
		for(TrainStation t : trainStationList){
			// 获取 车次编号+站序
			String train_code_index = t.getTrainCode() + "-" + t.getIndex();
			// 获取 车次编号+站名
			String train_code_name = t.getTrainCode() + "-" + t.getName();

			// 获取新增 车次编号+站序
			String train_code_index_new = trainStationInsertDTO.getTrainCode() + "-" + trainStationInsertDTO.getIndex();
			// 获取新增 车次编号+站名
			String train_code_name_new = trainStationInsertDTO.getTrainCode() + "-" + trainStationInsertDTO.getName();

			// 判断是否存在，如果存在则抛出异常
			if(train_code_index.equals(train_code_index_new)){
				throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_INDEX_UNIQUE_ERROR);
			}
			// 判断是否存在，如果存在则抛出异常
			if(train_code_name.equals(train_code_name_new)){
				throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_NAME_UNIQUE_ERROR);
			}
		}

		// 新增火车车站
		TrainStation trainStation = BeanUtil.copyProperties(trainStationInsertDTO, TrainStation.class);
		trainStation.setId(SnowUtil.getSnowflakeNextId());
		trainStation.setCreateTime(new Date());
		trainStation.setUpdateTime(new Date());
		trainStationMapper.insert(trainStation);
	}
}
